﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PlayerTest.CustomControls
{
	/// <summary>
    /// ImageStringToggleButtonControl.xaml에 대한 상호 작용 논리
	/// </summary>
	public partial class ImageStringToggleButtonControl : UserControl
	{
        private string backupDefaultImage = null;
        public ImageStringToggleButtonControl()
		{
			this.InitializeComponent();
            backupDefaultImage = DefaultImage;
            Opacity = 0.4;
		}

        #region 프로퍼티 연결

        // 기본 출력 이미지
        public static readonly DependencyProperty DefaultImageProperty =
            DependencyProperty.Register(
           "DefaultImage",
           typeof(string),
           typeof(ImageStringToggleButtonControl),
           //new FrameworkPropertyMetadata(false, ChangeDefaultImageProperty));
           new PropertyMetadata(null, new PropertyChangedCallback(ChangeDefaultImageProperty)));

        // 클릭시 출력 이미지
        public static readonly DependencyProperty PressedImageProperty =
            DependencyProperty.Register(
           "PressedImage",
           typeof(string),
           typeof(ImageStringToggleButtonControl),
           //new FrameworkPropertyMetadata(false, ChangePressedImageProperty));
           new PropertyMetadata(null, new PropertyChangedCallback(ChangePressedImageProperty)));

        // Checked
        public static readonly DependencyProperty CheckedProperty =
            DependencyProperty.Register(
           "Checked",
           typeof(bool),
           typeof(ImageStringToggleButtonControl),
           new FrameworkPropertyMetadata(false, ChangeCheckedProperty));
           //new PropertyMetadata(null, new PropertyChangedCallback(ChangeCheckedProperty)));

        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register(
           "Content",
           typeof(string),
           typeof(ImageStringToggleButtonControl),
           //new FrameworkPropertyMetadata(false, ChangeCheckedProperty));
           new PropertyMetadata(null, new PropertyChangedCallback(ChangeContentProperty)));

        #endregion

        public string DefaultImage
        {
            get { return (string)GetValue(DefaultImageProperty); }
            set { SetValue(DefaultImageProperty, value); }
        }
        public string PressedImage
        {
            get { return (string)GetValue(PressedImageProperty); }
            set { SetValue(PressedImageProperty, value); }
        }
        public bool Checked
        {
            get { return (bool)GetValue(CheckedProperty); }
            set { SetValue(CheckedProperty, value); }
        }
        
        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }
        

        private static void ChangeDefaultImageProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageStringToggleButtonControl button = d as ImageStringToggleButtonControl;

            if (button != null)
            {
                button.ImageStringToggleButtonCtrl.DefaultImage = (string)e.NewValue;
                button.backupDefaultImage = button.ImageStringToggleButtonCtrl.DefaultImage;
            }
        }

        private static void ChangePressedImageProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageStringToggleButtonControl button = d as ImageStringToggleButtonControl;

            if (button != null)
            {
                button.ImageStringToggleButtonCtrl.PressedImage = (string)e.NewValue;
            }
        }

        private static void ChangeCheckedProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageStringToggleButtonControl button = d as ImageStringToggleButtonControl;
            
            if (button != null)
            {
                button.Checked = (bool)e.NewValue;

                // 이미지 교체
                if (button.Checked)
                {
                    button.Opacity = 1.0;
                    string backup = button.ImageStringToggleButtonCtrl.DefaultImage;
                    button.ImageStringToggleButtonCtrl.DefaultImage = button.ImageStringToggleButtonCtrl.PressedImage;
                    button.backupDefaultImage = backup;
                }
                else
                {
                    button.Opacity = 0.4;
                    button.ImageStringToggleButtonCtrl.DefaultImage = button.backupDefaultImage;
                }

            }
        }

        private static void ChangeContentProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageStringToggleButtonControl button = d as ImageStringToggleButtonControl;

            if (button != null)
            {
                button.ImageStringToggleButtonCtrl.Content = (string)e.NewValue;
            }
        }
        
        public delegate void ClickHandler(object sender, RoutedEventArgs e);
        public event ClickHandler Click;

        public void Button_Click(object sender, RoutedEventArgs e)
        {
            //execute daddy's button click
            //(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
            //if (Click != null) Click(this, EventArgs.Empty);
            if (Click != null) Click(this, e);
            e.Handled = false;

            // 이미지 교체
            /*
            if (this.Checked)
            {
                this.Checked = false;
            }
            else
            {
                this.Checked = true;
            }
             */
        }

        private void ImageToggleButton_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Hand;
        }

        private void ImageToggleButton_MouseLeave(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = null;
        }
	}
}